<template>
  <section>
    <el-form label-width="100px">
      <el-form-item v-for="d in item.data.vals" :key="d.code" :label="d.title">
        <div  v-if="d.code !== 'currPhysical'">
          <el-select v-model="d.val" size="mini" @change="selPhysical(item.data.vals,d.val)" v-if="d.code === 'name'" value-key="id">
            <el-option v-for="(physical,i) in physicalList" :key="i" :value="physical" :label="physical.name"></el-option>
          </el-select>
          <div v-else>{{d.val?d.val:''}}</div>
        </div>
        <div  v-if="d.code == 'currPhysical'">
          <el-select v-model="d.val" size="mini" multiple>
            <el-option v-for="(physical,i) in tacticsList" :key="i" :value="physical.name" :label="physical.name" ></el-option>
          </el-select>
          <!--<div>-->
          <!--<span  v-for="v in d.val" :key="v.id"> [ {{v}} ] ; </span>-->
          <!--</div>-->
        </div>
      </el-form-item>
      <el-form-item>
        <div>
          <el-button size="mini" type="primary" @click="savePage(item)"  v-if="item.state === 0">测试·保存</el-button>
          <el-button type="danger" size="mini" @click="delPage">删除</el-button>
          <el-button type="success" size="mini" @click="finishePage" style="float: right;margin-right: 0px"  v-if="item.state === 0">完成</el-button>
        </div>
        <div v-if="item.state === 1">
          <span style="color: red;font-weight: bold">
          报告项已创建完毕
          </span>
        </div>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler.js'
  export default {
    name: "No2",
    props: ['item','report'],
    data() {
      return {
        physicalList:null,
        tacticsList:[]
      }
    },
    created(){
      //获取策略列表
      console.log(this.item)
      this.getTacticsList()
      if(this.item.data.vals){
      }else {
        Vue.set(this.item.data,'vals',this.item.reportModule.metadata.detail)
      }
    },
    mounted(){
      this.getPhysicalList()
    },
    computed:{
    },
    watch: {
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      selPhysical(vals,tmp){
        console.log(vals,tmp)
        // let tmp = this.physicalList.find(p=>p.id===id)
        vals.forEach(v=>{
          for(let key in tmp){
            if(key === v.code && key !=='name'){
              Vue.set(v,'val',tmp[key])
              // v.val=tmp[key]
            }
          }
        })
      },
      savePage(item){
        console.log(item)
        this.$emit('save')
      },
      delPage(){
        this.$emit('del')
      },
      finishePage(){
        this.$emit('finish')
      },
      getPhysicalList(){
        this.fetch({name:"getPhysicalList"})
          .then(res=>{
            console.log(res)
            this.physicalList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getTacticsList(){
        this.fetch({name:"getTacticsList"})
          .then(res=>{
            console.log(res)
            this.makeTacticsList(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      makeTacticsList(t){//制作所有子级策略数据
        t.forEach(i=>{
          if(!i.childrens){
            this.tacticsList.push(i)
          }else {
            this.makeTacticsList(i.childrens)
            return
          }
        })
        console.log(this.tacticsList)
      },
    }
  }
</script>

<style scoped>

</style>
